﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using wdss.WebServices;


namespace wdss.GUI
{
    public partial class Update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
            Response.Expires = -1500;
            Response.CacheControl = "no-cache";
            username_lbl.Text = (string)Session["username"];
            username_lbl.Visible = true;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.Now);
            Response.Cache.SetNoServerCaching();
            Response.Cache.SetNoStore();


            //check for if user should not be here
            try
            {
                if ((int)Session["role"] == 1)
                {
                    Server.Transfer("~/GUI/Login.aspx");
                }
                if ((int)Session["role"] == 3)
                {
                    Server.Transfer("~/GUI/SystemAdministrator.aspx");
                }
            }
            catch (HttpException)
            {
                Server.Transfer("~/GUI/Error.aspx");
            }
            catch (Exception)
            {
                Server.Transfer("~/GUI/Login.aspx");
            }

        }
        protected void Update_Button_Click(object sender, EventArgs e)
        {
            LogService logAction = new LogService();
            ListItem selectedItem = ddl_ChooseDocument.SelectedItem;
            String fileName = selectedItem.Text;
            int index = fileName.IndexOf(".");
            fileName = fileName.Remove(index);
            FileUpload filePath = FileUploadPath;
            UpdateService updateServiceObj = new UpdateService();
            try
            {
                String returnVal = updateServiceObj.UpdateFileService(fileName, filePath, (int)Session["userid"], Int32.Parse(ddl_ChooseDocument.SelectedValue));
                if (returnVal == "Success.")
                {
                    lbl_Success.Visible = true;
                    lbl_Error.Visible = false;
                    logAction.LogAction("User " + (string)Session["username"] + " successfully updated a file.");
                }
                else
                {
                    lbl_Success.Visible = false;
                    lbl_Error.Visible = true;
                    lbl_Error.Text = "Database error.";
                    logAction.LogAction("User " + (string)Session["username"] + " failed to update a file due to " + returnVal + ".");
                }
            }
            catch (ArgumentNullException ex)
            {
                lbl_Success.Visible = false;
                lbl_Error.Visible = true;
                lbl_Error.Text = "No document found.";
            }
            catch (FormatException ex)
            {
                lbl_Success.Visible = false;
                lbl_Error.Visible = true;
                lbl_Error.Text = "Not an integer based document.";
            }
            catch (OverflowException ex)
            {
                lbl_Success.Visible = false;
                lbl_Error.Visible = true;
                lbl_Error.Text = "Cannot convert document value, too large.";
            }
            catch (HttpException ex)
            {
                //no userid means no login, get this guy out of here
                Server.Transfer("~/GUI/Login.aspx");
            }
        }

    }
}
